var __import__ = function(url, callback) {
    var xhr = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
    xhr.open('GET', url, false);
    xhr.send(null);
    if (xhr.status == 200) {
        eval(xhr.responseText);
    }
};

/*
__import__(
    document.location.protocol + "//" + 
    document.location.host + 
    (document.location.port ? ":" + document.location.port : "") + 
    (document.location.pathname.replace(RegExp("\.html$"), "\.js").replace(RegExp("/$"), "/index.js"))
)
*/
//Functional.install()


$.qs = function() {
    o = Object();
    $.map(String(location.search).slice(1).split("&"),
                 function (f) {
                     a = f.split("=");
                     o[a[0]] = a[1];
                 }
                );
    return o;
};

// TODO:  cull make_element, replace OPTION, HIDDEN with ELEMENT partials
make_element = function(name, attrs, contents) {
    e = document.createElement(name);
    for (k in attrs) {
        e[k] = attrs[k];
    }
    e.innerHTML = contents;
    return e;
};
OPTION = function (attrs, contents) {
    return make_element('option', attrs, contents);
};
HIDDEN = function(id, value) {
    var e =  document.createElement('input');
    e.type = 'hidden';
    e.value = value;
    e.id = e.name = id;
    return e;
};


// more from mochkit
attr_renames = {
    'class':'className',
    'checked':'defaultChecked',
    'usemap':'useMap',
    'for':'htmlFor',
    'readonly':'readOnly',
    'colspan':'colSpan',
    'rowspan':'rowSpan',
    'bgcolor':'bgColor',
    'cellspacing':'cellSpacing',
    'cellpadding':'cellPadding'
};


fix_attr = function (item) {
    var rep = attr_renames[item[0]];
    if (rep && $.browser.msie) {
        return [rep, item[1]];
    }
    return item;
};


ELEMENT = function(tag, params, text) {
    var element = document.createElement(tag);
    var attrs = map(fix_attr, filter("x -> x[1]", items(params)));
    map(function (item) {element.setAttribute(item[0], item[1]);}, attrs);
    try { $(element).attr("style", params.style);} catch (e) {/* IE FAIL FAIL FAIL */}
    $(element).text(text);
    return element;
};
B = ELEMENT.partial("b");
DIV = ELEMENT.partial("div");
H1 = ELEMENT.partial("h1");
H2 = ELEMENT.partial("h2");
TABLE = ELEMENT.partial("table");
TD = ELEMENT.partial("td");
TR = ELEMENT.partial("tr");



var on_basic_err = function(msg, req, status, exc) {
  console.warn(msg);
};

// copied from mochikit 

keys = function (obj) {
    var rval = [];
    for (var prop in obj) {
        rval.push(prop);
	}
    return rval;
};

values = function (obj) {
    var rval = [];
    for (var prop in obj) {
        rval.push(obj[prop]);
    }
    return rval;
};


items = function (obj) {
    var rval = [];
    var e;
    for (var prop in obj) {
        var v;
        try {
            v = obj[prop];
        } catch (e) {
            continue;
        }
        rval.push([prop, v]);
    }
    return rval;
};

Array.prototype.unique = function( b ) {
    indexOf = function( v, b, s ) {
        for( var i = +b || 0, l = this.length; i < l; i++ ) {
            if( this[i]===v || s && this[i]==v ) { return i; }
        }
        return -1;
    };
    var a = [], i, l = this.length;
    for( i=0; i<l; i++ ) {
        if( a.indexOf( this[i], 0, b ) < 0 ) { a.push( this[i] ); }
    }
    return a;
};
